clear;clc;

%% USER PARAMETERS
Bx_SCAN_RATE = 2; % scan frequency [Hz]
Bx_SCAN_FROM = 0.0; Bx_SCAN_TO = 0.5;  % ramp bx field [V]

Bz_SCAN_NUMBER = 100;
Bz_SCAN_FROM = 0.0; Bz_SCAN_TO = 0.4;  % ramp bz field [V]

NI_SAMPLING_TIME = 50; % total recording time [s]
NI_SAMPLING_RATE = 50e3; % sampling rate [Sa/s]
NI_DISPLAY_TIME = 1./Bx_SCAN_RATE;   % display time for each scan [s]

%% Wire Connections
%
% * AFT_Rigol/Output 1 => AOM Modulation
% * AFT_Rigol/Sync 1 => ZI/Ref 1
%
% * AFG_Tek/Output 1 => Current Source Bx MOD IN
% * AFG_Tek/Output 2 => Current Source Bx(1) MOD IN
% * AFG_Tek/Trigger Out |=> ZI/Ref 2
%                       |=> NI/PFI0
%
% * ZI/Signal Input 1 <= PD/Pump
% * ZI/Signal Input 2 <= PD/Prob
% * ZI/Ref 1 <= AFG_Rigol/Sync 1
% * ZI/Ref 2 <= AFG_Tek/Trig output
% * ZI/Aux OUT 1 => NI/AI4
%
% * NI/AI0 <= Current Source Bx CTL OUT
% * NI/AI1 <= Current Source By CTL OUT
% * NI/AI2 <= Current Source Bz(1) CTL OUT
% * NI/AI3 <= Current Source Bz(2) CTL OUT
% * NI/AI4 <= ZI/Aux OUT 1


%% Device Setting
connect_devices;

%% AOM control
aom = EquipmentControl.AOM.AA('Pump Modulation');
aom.controlledBy(rigol, 1); pause(0.5);
aom.dcON();


%% field sweep

cs_bx  = EquipmentControl.CurrentSource.ThorlabsLDC201('Bx');
cs_bz1 = EquipmentControl.CurrentSource.ThorlabsLDC202('Bz1');


cs_bx.controlledBy(tek, 1).scan(Bx_SCAN_FROM, Bx_SCAN_TO, Bx_SCAN_RATE, '<>');

Bz_SCAN_RATE = Bx_SCAN_RATE/Bz_SCAN_NUMBER;
cs_bz1.controlledBy(tek, 2).scan(Bz_SCAN_FROM, Bz_SCAN_TO, Bz_SCAN_RATE);
tek.fire();

%% ZI
% input signal 1
ziDAQ('setDouble', '/dev2127/sigins/0/range', 1.5);
ziDAQ('setDouble', '/dev2127/sigins/0/scaling', 1);
ziDAQ('setInt', '/dev2127/sigins/0/imp50', 0);
ziDAQ('setInt', '/dev2127/sigins/0/ac', 0);

% input signal 2
ziDAQ('setDouble', '/dev2127/sigins/1/range', 1.5);
ziDAQ('setDouble', '/dev2127/sigins/1/scaling', 1);
ziDAQ('setInt', '/dev2127/sigins/1/imp50', 0);
ziDAQ('setInt', '/dev2127/sigins/1/ac', 0);

% set external reference
ziDAQ('setInt', '/dev2127/extrefs/0/enable', 0);
ziDAQ('setInt', '/dev2127/demods/0/adcselect', 0);
ziDAQ('setInt', '/dev2127/demods/1/adcselect', 1);

% set scope
ziDAQ('setInt', '/dev2127/scopes/0/time', 16); % sampling rate
ziDAQ('setDouble', '/dev2127/scopes/0/length', 32768); % record length
ziDAQ('setInt', '/dev2127/scopes/0/channels/0/inputselect', 0); % ch 1 (probe)
ziDAQ('setInt', '/dev2127/scopes/0/channels/1/inputselect', 1); % ch 2 (pump)
ziDAQ('setInt', '/dev2127/scopes/0/trigchannel', 3); % trigger
ziDAQ('setInt', '/dev2127/scopes/0/channel', 3); % enable both channels 
ziDAQ('setInt', '/dev2127/scopes/0/enable', 1); % start

pause(1);
ziDAQ('setInt', '/dev2127/sigins/0/autorange', 1);
ziDAQ('setInt', '/dev2127/sigins/1/autorange', 1);

% set aux output
% ziDAQ('setInt', '/dev2127/auxouts/0/demodselect', 1); %select demod 2 (pump)
% ziDAQ('setInt', '/dev2127/auxouts/0/outputselect', 2); %select demod R
% ziDAQ('setDouble', '/dev2127/auxouts/0/scale', 100); % scale = 100


%% NI
ni.addInputChannel(0:5);
ni.addExternalTrigger('PFI0', 'StartTrigger');
ni.setSampling(NI_SAMPLING_TIME, NI_SAMPLING_RATE, NI_DISPLAY_TIME).enableDataKeep();
ni.startScope();

tek.forceTrigger();
ni.wait();pause(1);

% tek.silence();

%% 2D plot
scanList = 1:Bz_SCAN_NUMBER;
timeList = ni.timeStampsSet(:, 1);
ch0Data = ni.dataSet(:, 1:6:end).';
ch2Data = ni.dataSet(:, 3:6:end).';
ch4Data = ni.dataSet(:, 5:6:end).';
ch5Data = ni.dataSet(:, 6:6:end).';
[X, Y] = meshgrid(timeList, scanList);

figure;
subplot(2, 2, 1);
contourf(X,Y,ch0Data, 50, 'LineStyle', 'None');colorbar
title('Bx (current source output V)'); xlabel('scan time (s)'); ylabel('scan number');
subplot(2, 2, 2);
contourf(X,Y,ch2Data, 50, 'LineStyle', 'None');colorbar
title('Bz (current source output V)'); xlabel('scan time (s)'); ylabel('scan number');
subplot(2, 2, 3);
contourf(X,Y,ch4Data, 50, 'LineStyle', 'None');colorbar
title('Pump Signal'); xlabel('scan time (s)'); ylabel('scan number');
subplot(2, 2, 4);
contourf(X,Y,ch5Data, 50, 'LineStyle', 'None');colorbar
title('Probe Signal'); xlabel('scan time (s)'); ylabel('scan number');